(function ($, doc) {
	$.init();
    mui("#feedback").on("tap", "#submit", function() {
        var user    = app.getState();
        var content = document.getElementById('question').value;
        var contact = document.getElementById('contact').value;
        var toUrl   = apiurl + 'System/addFeedback';
        if(content == ''){
            $.alert('请详细描述你的问题和意见');
            return false;
        }
        mui.post(toUrl, {
            u_id : user.user_id,fb_content : content,extra : contact
        }, function(res){
            if(res.status == 1){
                var len = document.getElementById('span1').children.length;
                var img = new Array();
                for(var j = 0; j < len; j++){
                    img[j] = document.querySelector('#span1 .img'+j).getAttribute('src');
                    uploadImg(img[j], res.id);
                }
            }
        })
    })

    function uploadImg(path, id) {
        var img = new Image();
        img.src = path;
        img.onload = function() {
            var that = this;
            //生成比例
            var w = that.width,
                h = that.height,
                scale = w / h;
            w = 480 || w; //480  你想压缩到多大
            h = w / scale;

            //生成canvas
            var canvas = document.createElement('canvas');
            var ctx = canvas.getContext('2d');
            canvas.setAttribute('width', w);
            canvas.setAttribute('height', h);
            ctx.drawImage(that, 0, 0, w, h);
            var base64 = canvas.toDataURL('image/jpeg', 1 || 0.8); //1z 表示图片质量，越低越模糊。
            base64 = base64.replace('data:image/jpeg;base64,', '');
            $.post(apiurl + 'System/upload', {
                file: base64,
                type: 'feedback',
                id: id
            }, function(res) {
                if(res.status > 0) {
                    var preView = plus.webview.getWebviewById('problem_feedback');
                    mui.fire(preView,'ADDFEEDBACK');
                    plus.webview.currentWebview().close();
                } else {

                }
            });
        }
    };

    $.plusReady(function () {
        mui(".mui-page-content").on("tap", "#uploadimg", function() {
            plus.nativeUI.actionSheet({
                title: "请选择上传方式",
                cancel: "取消",
                buttons: [ {
                    title: "相册"
                },{
                    title: "拍照"
                }]
            }, function(e) {
                if(e.index == 1) {
                    galleryMaximum();
                }else if(e.index == 2) {
                    getImage();
                }
                return true;
            })
        })

        // 从相册中选择多张图片
        function galleryMaximum(){
            // 从相册中选择图片
            console.log("从相册中选择多张图片:");
            plus.gallery.pick( function(e){
                var len = parseInt(document.getElementById('span1').getElementsByTagName('img').length);
                for(var i in e.files){
                    var img = document.createElement('img');
                    img.src = e.files[i];
                    img.style.width 	= '22%';
                    img.style.height 	= '100px';
                    img.className 		= 'img'+len;
                    img.style.paddingRight = '5px';
                    document.querySelector('#image-list .span1').appendChild(img);
                    len++;
                }
                if(len >= 4){
                    document.querySelector('#uploadimg').style.display = 'none';
                }
            }, function ( e ) {
                console.log( "取消选择图片" );
            },{filter:"image",multiple:true,maximum:1,system:false,onmaxed:function () {
                // plus.nativeUI.alert('最多只能选择1张图片');
            }});// 最多选择9张图片
        }

        function getImage() {
            var c = plus.camera.getCamera();
            c.captureImage(function(e) {
                plus.io.resolveLocalFileSystemURL(e, function(entry) {
                    var s = entry.toLocalURL() + "?version=" + new Date().getTime();
                    var len = parseInt(document.getElementById('span1').getElementsByTagName('img').length);
                    var img = document.createElement('img');
                    img.src = s;
                    img.style.width 	= '22%';
                    img.style.height 	= '100px';
                    img.className 		= 'img'+len;
                    img.style.paddingRight = '5px';
                    document.querySelector('#image-list .span1').appendChild(img);
                }, function(e) {
                    console.log("读取拍照文件错误：" + e.message);
                });
            }, function(s) {
                console.log("error" + s);
            }, {
                filename: "_doc/camera"
            })
        }
    })


}(mui, document));